<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class AdApp extends BackendApp
{
	var $_ad_mod;
	var $_uploadedfile_mod;

	function __construct()
	{
		$this->AdApp();
	}

	function AdApp()
	{
		parent::BackendApp();

		$this->_ad_mod =& m('ad');
		$this->_uploadedfile_mod = &m('uploadedfile');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
		$dictionary_mod = & m('dictionary');
		$conditions='';
		$conditions .= $this->_get_query_conditions(array(
		array(
                'field' => 'title',         //可搜索字段title
                'equal' => 'LIKE',          //等价关系,可以是LIKE, =, <, >, <>
                'assoc' => 'AND',           //关系类型,可以是AND, OR
                'name'  => 'title',         //GET的值的访问键名
                'type'  => 'string',        //GET的值的类型
		),
		array(
                'field' => 'type',                
                'assoc' => 'AND',
                'name' => 'type',
                'type' => 'string',
		),
		array(
                'field' => 'page',                
                'assoc' => 'AND',
                'name' => 'pages',
                'type' => 'string',
		),
		array(
                'field' => 'position',                
                'assoc' => 'AND',
                'name' => 'position',
                'type' => 'string',
		),
		));
		$page   =   $this->_get_page(10);    //获取分页信息

		$sql="SELECT COUNT(1) from zys_ad ca
			LEFT JOIN   zys_dictionary cd1 on ca.position=cd1.d_value
			LEFT JOIN   zys_dictionary cd2 on ca.page=cd2.d_value
			LEFT JOIN   zys_dictionary cd3 on ca.type=cd3.d_value
			WHERE 1=1 ".$conditions." ORDER BY ca.ad_tid DESC ";
		$count=$this->_ad_mod->getOne($sql);


		$sql="SELECT  ca.*,cd1.d_key as dposition,cd2.d_key as dpage,cd3.d_key as dtype from  zys_ad  ca
			LEFT JOIN   zys_dictionary cd1 on ca.position=cd1.d_value
			LEFT JOIN   zys_dictionary cd2 on ca.page=cd2.d_value
			LEFT JOIN   zys_dictionary cd3 on ca.type=cd3.d_value
			WHERE 1=1 ".$conditions." ORDER BY ca.ad_tid DESC LIMIT ".$page['limit'];
		$articles=$this->_ad_mod->getAll($sql);


		$page['item_count'] = $count;   //获取统计的数据
		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条


		$sql_page = 'select d_key,d_value from zys_dictionary where d_code = "ad_page"';
		$page_index = $dictionary_mod->getAll($sql_page);
		$sql_position = 'select d_key,d_value from zys_dictionary where d_code = "ad_position"';
		$position_index = $dictionary_mod->getAll($sql_position);
		$this->assign('page_index', $page_index);
		$this->assign('position_index', $position_index);


		$sql="select  * from   zys_dictionary where  d_code='ad_type'";
		$dis_list = $this->_ad_mod->GetAll($sql);
		$dis_cat=array();
		foreach ($dis_list as $k=>$v){
			$dis_cat[$v['d_value']]=$v['d_key'];
		}
		$this->assign('dis_cat', $dis_cat);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->assign('articles', $articles);
		$this->display('ad.index.html');
	}
	/**
	 *    新增文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function add()
	{
		if (!IS_POST)
		{
			/* 显示新增表单 */

			$activity = array('sort_order' => 255);
			$this->assign("id", 0);
			$this->assign('activity', $activity);

			$sql="select  * from   zys_dictionary where  d_code='ad_type'";//类型
			$dis_list = $this->_ad_mod->GetAll($sql);
			$dis_cat=array();
			foreach ($dis_list as $k=>$v){
				$dis_cat[$v['d_value']]=$v['d_key'];
			}
			//$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->import_resource(array('script' => 'mlselection.js,jquery.plugins/jquery.validate.js,change_upload.js,inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->assign('dis_cat', $dis_cat);

			$dis_position=array();
			$sql="select  * from   zys_dictionary where  d_code='ad_position'";//位置
			$dis_list = $this->_ad_mod->GetAll($sql);
			foreach ($dis_list as $k=>$v){
				$dis_position[$v['d_value']]=$v['d_key'];
			}
			$this->assign('dis_position', $dis_position);

			$dis_page=array();
			$sql="select  * from   zys_dictionary where  d_code='ad_page'";//页面
			$dis_list = $this->_ad_mod->GetAll($sql);
			foreach ($dis_list as $k=>$v){
				$dis_page[$v['d_value']]=$v['d_key'];
			}
			$this->assign('dis_page', $dis_page);
			$this->display('ad.form.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			$data['position']    =   $_POST['position'];
			$data['position']    =   $_POST['position'];
			$data['page']    =   $_POST['page'];
			$data['is_show']    =   $_POST['is_show'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['content'] =   $_POST['article_content'];
			$data['url']    =   $_POST['url'];
			$data['outkey']    =   $_POST['outkey'];


			if (!$article_id = $this->_ad_mod->add($data))  //获取article_id
			{
				$this->show_warning($this->_ad_mod->get_error());
				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_ad_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下
			$this->show_message('add_ad_successed',
                'back_list',    'index.php?app=ad',
                'continue_add', 'index.php?app=ad&amp;act=add'
                );
		}
	}
	/**
	 *    编辑文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function edit()
	{
		$article_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$article_id)
		{
			$this->show_warning('no_such_activity','','index.php?app=ad');
			return;
		}
		if (!IS_POST)
		{
			$activity     = $this->_ad_mod->find($article_id);
			$activity    =   current($activity);
			if($activity['type']!='url'){

				if($activity['outkey']){
					if($activity['type']=='store'){
						$sql=" SELECT  store_name  as  tname from   zys_store where  store_id=".$activity['outkey'];
					}elseif($activity['type']=='goods'){
						$sql="SELECT  goods_name from  zys_goods  where  goods_id=".$activity['outkey'];
					}elseif($activity['type']=='article'){
						$sql="SELECT  title  as  tname from   zys_article  where  article_id=".$activity['outkey'];
					}
					$tname = $this->_ad_mod->GetOne($sql);
				}

			}
			$activity['tname']=$tname;
			if (empty($activity))
			{
				$this->show_warning('no_such_ad','','index.php?app=ad');
				return;
			}


			$dis_position=array();
			$sql="select  * from   zys_dictionary where  d_code='ad_position'";//位置
			$dis_list = $this->_ad_mod->GetAll($sql);
			foreach ($dis_list as $k=>$v){
				$dis_position[$v['d_value']]=$v['d_key'];
			}
			$this->assign('dis_position', $dis_position);

			$dis_page=array();
			$sql="select  * from   zys_dictionary where  d_code='ad_page'";//页面
			$dis_list = $this->_ad_mod->GetAll($sql);
			foreach ($dis_list as $k=>$v){
				$dis_page[$v['d_value']]=$v['d_key'];
			}
			$this->assign('dis_page', $dis_page);
			$this->import_resource(array('script' => 'mlselection.js,jquery.plugins/jquery.validate.js,change_upload.js,inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

			$this->assign('activity', $activity);
			$this->display('ad.form.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			if($_POST['type']=='url'){
				$data['outkey']    = 0;
			}else{
				$data['outkey']    =   $_POST['outkey'];
			}
			$data['position']    =   $_POST['position'];
			$data['page']    =   $_POST['page'];
			$data['is_show']    =   $_POST['is_show'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['content'] =   $_POST['article_content'];
			$data['url']    =   $_POST['url'];

			$rows=$this->_ad_mod->edit($article_id, $data);
			if ($this->_ad_mod->has_error())
			{
				$this->show_warning($this->_ad_mod->get_error());
				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_ad_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下

			$this->show_message('edit_ad_successed',
                'back_list',        'index.php?app=ad',
                'edit_again',    'index.php?app=ad&amp;act=edit&amp;id=' . $article_id);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('is_show', 'sort_order')))
		{
			$data[$column] = $value;
			$this->_ad_mod->edit($id, $data);
			if(!$this->_ad_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	function drop()
	{
		$article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$article_ids)
		{
			$this->show_warning('no_such_ad');

			return;
		}
		$article_ids=explode(',', $article_ids);
		$this->_ad_mod->drop($article_ids);
		if ($this->_ad_mod->has_error())    //删除
		{
			$this->show_warning($this->_activity_mod->get_error());

			return;
		}

		$this->show_message('drop_ok','','index.php?app=ad');
	}


	//广告选择分类
	function   sel_type(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$name=  isset($_GET['name']) ? trim($_GET['name']) : '';
		if (!$id)
		{
			$this->show_warning('no_such_ad');
			return;
		}
		$sqlw='';
		$type= isset($_GET['type']) ? trim($_GET['type']) : '';
		if($type=='store'){
			if($name){
				$sqlw=" and store_name  like '%$name%' or  owner_name  like '%$name%' ";
			}
			$sql="select  * from  zys_store where  1=1  ".$sqlw;
			$list=$this->_ad_mod->db->getAll($sql);

		}elseif ($type=='goods'){
			if($name){
				$sqlw=" and goods_name  like '%$name%' ";
			}
			$sql="select  * from  zys_goods where  1=1  ".$sqlw;
			$list=$this->_ad_mod->db->getAll($sql);
		}elseif ($type=='article'){
			if($name){
				$sqlw=" and title  like '%$name%' ";
			}
			$sql="select  * from  zys_article where  1=1  ".$sqlw;
			$alist=$this->_ad_mod->db->getAll($sql);
			$list=array();
			foreach ($alist as $k=>$v){
				if($v['add_time']){
					$v['add_time']=date('y-m-d H:s:m',$v['add_time']);
				}
				$list[]=$v;
			}
		}
		$this->assign('filtered', $sqlw? 1 : 0); //是否有查询条件
		$this->assign('list', $list);
		$this->assign('type', $type);
		$this->assign('id', $id);
		$this->display('ad_type.index.html');

	}





	function _upload_logo($brand_id)
	{
		$file = $_FILES['pic'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['pic']);//上传logo

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=activity&amp;act=edit&amp;id=' . $brand_id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/activity', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
}

?>